package com.shelpe.services.algorithm.entity.mongo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;

import com.shelpe.services.algorithm.entity.jpa.Adgroup;
import com.shelpe.services.algorithm.entity.jpa.AutoProductQueue;
import com.shelpe.services.algorithm.entity.jpa.DevelopmentPlan;

import lombok.Data;

@Data
@Document(collection = "mobile_qscore_action_log")
public class MobileQscoreActionLog {

	@Id
	private String logID;

	@Indexed
	private long adgroupID;
	@Indexed
	private long campaignID;
	private Adgroup adgroup;
	@Indexed
	private Date executeTime;
	private int days;

	private DevelopmentPlan plan;

	private List<KeywordSnapshot> snapshots;

	private int averageMobileQscore;
	private float averageCtr;

	public void from(Date executeTime, AutoProductQueue queue, Adgroup adgroup) {
		this.adgroup = adgroup;
		this.executeTime = executeTime;
		if (adgroup != null) {
			this.campaignID = adgroup.getCampaignID();
			this.adgroupID = adgroup.getAdgroupID();
		}
	}

	public void addSnapshot(KeywordSnapshot ks) {
		if (this.snapshots == null) {
			this.snapshots = new ArrayList<KeywordSnapshot>();
		}
		this.snapshots.add(ks);
	}

	public KeywordSnapshot findByKeywordID(long keywordID) {

		if (this.snapshots != null) {
			for (KeywordSnapshot ks : this.snapshots) {
				if (ks.getKeywordID() == keywordID) {
					return ks;
				}
			}
		}

		return null;
	}

	@Data
	public static class KeywordSnapshot {
		private long keywordID;
		private String word;
		private int mobileQscore;

		private int realtimeRank;

		private String action;
		private String reason;
		private boolean result;
	}
}
